package com.yipei.business.controller;

import com.yipei.business.domain.MlAssetFlow;
import com.yipei.business.service.IMlAssetFlowService;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 资金流水Controller
 *
 * @author yipei
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/business/assetflow")
@Api(tags = "资金流水", description = "资金流水管理")
public class MlAssetFlowController extends BaseController
{
    @Autowired
    private IMlAssetFlowService mlAssetFlowService;

    /**
     * 查询资金流水列表
     */
    @ApiOperation("资金流水获取数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:list')")
    @GetMapping("/list")
    public TableDataInfo list(MlAssetFlow mlAssetFlow)
    {
        startPage();
        List<MlAssetFlow> list = mlAssetFlowService.selectMlAssetFlowList(mlAssetFlow);
        return getDataTable(list);
    }

    /**
     * 导出资金流水列表
     */
    @ApiOperation("资金流水导出数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:export')")
    @Log(title = "资金流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlAssetFlow mlAssetFlow)
    {
        List<MlAssetFlow> list = mlAssetFlowService.selectMlAssetFlowList(mlAssetFlow);
        ExcelUtil<MlAssetFlow> util = new ExcelUtil<MlAssetFlow>(MlAssetFlow.class);
        util.exportExcel(response, list, "资金流水数据");
    }

    /**
     * 获取资金流水详细信息
     */
    @ApiOperation("资金流水通过ID获取数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mlAssetFlowService.selectMlAssetFlowById(id));
    }

    /**
     * 新增资金流水
     */
    @ApiOperation("资金流水新增数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:add')")
    @Log(title = "资金流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlAssetFlow mlAssetFlow)
    {
        return toAjax(mlAssetFlowService.insertMlAssetFlow(mlAssetFlow));
    }

    /**
     * 修改资金流水
     */
    @ApiOperation("资金流水修改数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:edit')")
    @Log(title = "资金流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlAssetFlow mlAssetFlow)
    {
        return toAjax(mlAssetFlowService.updateMlAssetFlow(mlAssetFlow));
    }

    /**
     * 删除资金流水
     */
    @ApiOperation("资金流水删除数据")
    //@PreAuthorize("@ss.hasPermi('business:assetflow:remove')")
    @Log(title = "资金流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mlAssetFlowService.deleteMlAssetFlowByIds(ids));
    }

    @ApiOperation("获取资金流水数据")
//    @Log(title = "委托订单", businessType = BusinessType.INSERT)
    @GetMapping("/assetFlowData")
    public AjaxResult getAssetFlowData(@RequestParam(name = "s_time", required = true) String s_time, @RequestParam(name = "e_time", required = true) String e_time ) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        e_time= e_time+" 24:59:59";
        List<MlAssetFlow> list = mlAssetFlowService.lambdaQuery().eq(MlAssetFlow::getTdAcc,tdAcc)
                .ge(MlAssetFlow::getCreateTime,s_time).le(MlAssetFlow::getCreateTime,e_time).orderByDesc(MlAssetFlow::getCreateTime)
                .list();
        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.error("未获取到时间范围内成交订单！");
        }else {
            return AjaxResult.success(list);
        }
    }


    @ApiOperation("获取最近一个月资金流水数据")
//    @Log(title = "委托订单", businessType = BusinessType.INSERT)
    @GetMapping("/assetFlowDataMonth")
    public AjaxResult getAssetFlowMonthData(){
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LocalDate localDate = LocalDate.now().minusMonths(1);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        List<MlAssetFlow> list = mlAssetFlowService.lambdaQuery().eq(MlAssetFlow::getTdAcc,tdAcc)
                .ge(MlAssetFlow::getCreateTime,date).le(MlAssetFlow::getCreateTime,new Date()).orderByDesc(MlAssetFlow::getCreateTime)
                .list();
        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.error("未获取到时间范围内成交订单！");
        }else {
            return AjaxResult.success(list);
        }
    }
}
